package com.zbkj.service.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiniu.util.StringUtils;
import com.zbkj.common.constants.ProductConstants;
import com.zbkj.common.exception.AppException;
import com.zbkj.common.model.member.PaidMemberCard;
import com.zbkj.common.model.member.PaidMemberCardApply;
import com.zbkj.common.model.product.Product;
import com.zbkj.common.model.product.ProductAttrValue;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.PaidMemberCardApplyRequest;
import com.zbkj.common.request.PaidMemberCardSaveRequest;
import com.zbkj.common.response.CardBrandForApplyResponse;
import com.zbkj.common.response.ProductActivityResponse;
import com.zbkj.common.result.CommonResultCode;
import com.zbkj.service.dao.PaidMemberCardApplyDao;
import com.zbkj.service.dao.PaidMemberCardDao;
import com.zbkj.service.service.PaidMemberCardApplyService;
import com.zbkj.service.service.ProductAttrValueService;
import com.zbkj.service.util.BrandUtil;
import jodd.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author hzw
* @description PaidMemberCardServiceImpl 接口实现
* @date 2024-05-10
*/
@Service
public class PaidMemberCardApplyServiceImpl extends ServiceImpl<PaidMemberCardApplyDao, PaidMemberCardApply> implements PaidMemberCardApplyService {

    @Resource
    private PaidMemberCardApplyDao dao;
    @Resource
    private PaidMemberCardDao paidMemberCardDao;
    @Resource
    private ProductAttrValueService productAttrValueService;

    /**
     * 申请共享vip
     */
    @Override
    public Boolean apply(List<PaidMemberCardApplyRequest> request) {
       List<PaidMemberCardApply> cardList = new ArrayList<>();
       Integer brandId = BrandUtil.getBrandIdByUrl();
       Date date = new Date();
        request.forEach(item -> {
            PaidMemberCardApply card = new PaidMemberCardApply();
            BeanUtils.copyProperties(item, card);
            card.setCreateTime(date);
            card.setApplyBrandId(brandId);
            cardList.add(card);
        });

        boolean status = saveBatch(cardList);
        return status;
    }

    /**
     * 审核共享vip
     * @param request
     * @return
     */
    @Override
    public Boolean review(List<PaidMemberCardApplyRequest> request) {
        List<PaidMemberCardApply> cardList = new ArrayList<>();
        Date date = new Date();
        request.forEach(item -> {
            if (item.getId()!=null&&item.getStatus()!=null){
                PaidMemberCardApply card = new PaidMemberCardApply();
                BeanUtils.copyProperties(item, card);
                card.setReviewTime(date);
                cardList.add(card);
            }

        });
        boolean status = updateBatchById(cardList);
        return status;
    }

    @Override
    public List<CardBrandForApplyResponse> getForCardBrands(Integer cardId) {
        Integer brandId = BrandUtil.getBrandIdByUrl();
        PaidMemberCard paidMemberCard = paidMemberCardDao.selectById(cardId);
        List<CardBrandForApplyResponse> list = dao.getForCardBrands(brandId,cardId, paidMemberCard.getDiscount());
        return list;
    }

    @Override
    public List<CardBrandForApplyResponse> getApplyCardBrands(Integer cardId) {
        List<CardBrandForApplyResponse> list = dao.getApplyCardBrands(cardId);
        return list;
    }

    @Override
    public PageInfo<ProductActivityResponse> discount(PageParamRequest pageRequest) {
        Integer brandId = BrandUtil.getBrandIdByUrl();
        Page<Product> page = PageHelper.startPage(pageRequest.getPage(), pageRequest.getLimit());
        List<ProductActivityResponse> responseList = dao.getDiscountSearchPage(brandId);
        responseList.forEach(response -> {
            List<ProductAttrValue> attrValueList = productAttrValueService.getListByProductIdAndType(response.getId(), ProductConstants.PRODUCT_TYPE_NORMAL);
            response.setAttrValue(attrValueList);
        });
        return CommonPage.copyPageInfo(page, responseList);
    }

    public List<PaidMemberCardApply> getCardApplys(Integer appliedForCardId) {
        LambdaUpdateWrapper<PaidMemberCardApply> wrapper = Wrappers.lambdaUpdate();
        //被申请人vip id
        wrapper.eq(PaidMemberCardApply::getAppliedForCardId, appliedForCardId);
        //被申请人品牌id
        Integer brandId = BrandUtil.getBrandIdByUrl();
        wrapper.eq(PaidMemberCardApply::getAppliedForBrandId, brandId);
        wrapper.eq(PaidMemberCardApply::getStatus, 1);
        List<PaidMemberCardApply> paidMemberCardApply = dao.selectList(wrapper);
        return paidMemberCardApply;
    }
}

